﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Data;
using GSSoftSL.Web;

namespace GSSoftSL.Utility
{
    public class EnumToIntConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Note: as pointed out by Martin in the comments on this answer, this line
            // depends on the enum values being sequentially ordered from 0 onward,
            // since combobox indices are done that way. A more general solution would
            // probably look up where in the GetValues array our value variable
            // appears, then return that index.
            return (int)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Enum.Parse(targetType, value.ToString(), true);
        }
    }
    public class EnumToIEnumerableConverter : IValueConverter
    {
        private Dictionary<Type, List<object>> cache = new Dictionary<Type, List<object>>();

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var type = value.GetType();
            if (!this.cache.ContainsKey(type))
            {
                var fields = type.GetFields().Where(field => field.IsLiteral);
                var values = new List<object>();
                foreach (var field in fields)
                {
                    DescriptionAttribute[] a = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    if (a != null && a.Length > 0)
                    {
                        values.Add(a[0].Description);
                    }
                    else
                    {
                        values.Add(field.GetValue(value));
                    }
                }
                this.cache[type] = values;
            }

            return this.cache[type];
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class GioiTinhConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return "Chọn...";
            if ((int)value == 0)
                return "Nữ";
            else
                return "Nam";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((string)value == "Nữ")
                return 0;
            else if ((string)value == "Nam")
                return 1;
            else
                return null;
            //return Enum.Parse(targetType, value.ToString(), true);
        }
    }

    public class TrangThaiConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((int)value == 0)
                return "Hoạt động";
            else
                return "Không hoạt động";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((string)value == "Hoạt động")
                return 0;
            else
                return 1;
            //return Enum.Parse(targetType, value.ToString(), true);
        }
    }

    public class BuoiHocConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;
            if ((int) value == 0)
                return "Sáng";
            else if ((int) value == 1)
                return "Chiều";
            else if ((int) value == 2)
                return "Tối";
            else
                return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((string)value == "Sáng")
                return 0;
            else if ((string)value == "Chiều")
                return 1;
            else if ((string)value == "Tối")
                return 2;
            else
                return null;
            //return Enum.Parse(targetType, value.ToString(), true);
        }
    }

    public class TimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((DateTime) value).ToString("HH:mm");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
           /* if ((string)value == "Sáng")
                return 0;
            else if ((string)value == "Chiều")
                return 1;
            else
                return 2;*/
            string[] tokens = value.ToString().Split(':');
            return new DateTime(2000, 1, 1, int.Parse(tokens[0]), int.Parse(tokens[1]), 0);
        }
    }

    public class NgayTrongTuanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            switch ((int)value)
            {
                case 0:
                    return "Chủ nhật";
                case 1:
                    return "Thứ 2";
                case 2:
                    return "Thứ 3";
                case 3:
                    return "Thứ 4";
                case 4:
                    return "Thứ 5";
                case 5:
                    return "Thứ 6";
                case 6:
                    return "Thứ 7";
            }
            return "Chủ nhật";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            switch((string) value)
            {
                case "Chủ nhật":
                    return 0;
                case "Thứ 2":
                    return 1;
                case "Thứ 3":
                    return 2;
                case "Thứ 4":
                    return 3;
                case "Thứ 5":
                    return 4;
                case "Thứ 6":
                    return 5;
                case "Thứ 7":
                    return 6;
            }
            return 0;
        }
    }

    public class DataFormEnableConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (DataFormMode) value == DataFormMode.ReadOnly;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class DataFormEditActionConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((DataFormEditEndedEventArgs) value).EditAction == DataFormEditAction.Commit;
            //return (DataFormMode)value == DataFormMode.ReadOnly;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class DateTimeVNConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            DateTime? date = (DateTime?) value;
            if (date.HasValue)
                return ((DateTime)value).ToString("dd/MM/yyyy");
            else
                return null;// new DateTime(1900, 1, 1);
            //return (DataFormMode)value == DataFormMode.ReadOnly;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            DateTime date;
            if (DateTime.TryParse(value.ToString(), new DateTimeFormatInfo() { ShortDatePattern = "dd/MM/yyyy" }, DateTimeStyles.None, out date))
                return date;
            //return new DateTime(1900, )
            DateTime? tmp = null;
            return tmp;
        }
    }

    public class DiemSoDecimalConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return "";
            return ((decimal)value).ToString("N2");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value.ToString().Trim() == "")
                return null;
            decimal res;
            if (decimal.TryParse(value.ToString(), out res))
                return res;
            else
                return null;
        }
    }

    public class NegativeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return !((bool) value);
            //return (DataFormMode)value == DataFormMode.ReadOnly;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return !((bool)value);
        }
    }

    public class TimeSpanToDateTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            TimeSpan time = (TimeSpan)value;
            return new DateTime(200, 1, 1, time.Hours, time.Minutes, 0);
            //return (DataFormMode)value == DataFormMode.ReadOnly;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            DateTime date = (DateTime)value;
            return new TimeSpan(0, date.Hour, date.Minute, 0);
        }
    }

    public class TimeSpanToTextConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            TimeSpan time = (TimeSpan)value;
            return parameter == null ? time.ToString("hh:mm") : time.ToString((string) parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            DateTime date = (DateTime)value;
            return new TimeSpan(0, date.Hour, date.Minute, 0);
        }
    }

    public class ComboboxSelectedIndexRelativeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((int) value) > 0;
            //return (DataFormMode)value == DataFormMode.ReadOnly;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class TrangThaiHocPhiConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int type = System.Convert.ToInt32(parameter);
            if (type == 0)
            {
                switch ((EnumTrangThaiHocPhi)value)
                {
                    case EnumTrangThaiHocPhi.HoanTat:
                        return "Hoàn tất";
                    case EnumTrangThaiHocPhi.NuaKhoa:
                        return "1/2 khóa";
                    default:
                        return "Chưa đóng";
                }
            }
            else
            {
                switch ((EnumTrangThaiHocPhi)value)
                {
                    case EnumTrangThaiHocPhi.HoanTat:
                        return "Trọn khóa";
                    case EnumTrangThaiHocPhi.NuaKhoa:
                        return "1/2 khóa";
                    default:
                        return "Chưa đóng";
                }
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int type = System.Convert.ToInt32(parameter);
            if (type == 0)
            {
                switch ((string)value)
                {
                    case "Hoàn tất":
                        return (int)EnumTrangThaiHocPhi.HoanTat;
                    case "1/2 khóa":
                        return (int)EnumTrangThaiHocPhi.NuaKhoa;
                    default:
                        return (int)EnumTrangThaiHocPhi.ChuaDong;
                }
            }
            else
            {
                switch ((string)value)
                {
                    case "Trọn khóa":
                        return (int)EnumTrangThaiHocPhi.HoanTat;
                    case "1/2 khóa":
                        return (int)EnumTrangThaiHocPhi.NuaKhoa;
                    default:
                        return (int)EnumTrangThaiHocPhi.ChuaDong;
                }
            }
        }
    }
}
